package com.woniu.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.woniu.pojo.Staff;
import com.woniu.pojo.User;
import com.woniu.service.StaffService;

@Controller
@RequestMapping("/staff")
public class StaffController {
	@Autowired
	StaffService staffService;
	@RequestMapping("/info")
	public String information(HttpServletRequest req) {

		return "/staff/staffInfo.jsp";
	}
	@RequestMapping("/update")
	public String update() {
		return  "redirect:/staff/staffUpdate.jsp";
	}
	@RequestMapping("/doupdate")
	public String doupdate(Staff staff,HttpServletRequest req) {
		
		staffService.updateStaff(staff);
		Staff newStaff = staffService.getStaffById(staff.getStaff_id());
		req.getSession().setAttribute("loginStaff", newStaff);
		return  "/staff/info";
	}
	@RequestMapping("/uploadimg")
	public String uploadheadimg(MultipartFile myheadimg,HttpServletRequest req) {
		System.out.println(myheadimg);
		Staff staff = (Staff)req.getSession().getAttribute("loginStaff");
		if (myheadimg!=null) {
			System.out.println(myheadimg.getOriginalFilename()+"------");
			String uploadDir = req.getRealPath("/upload/");
			System.out.println(uploadDir);
			File f = new File(uploadDir);
			if(!f.exists()) {
				f.mkdir();
			}
			String newFileName = System.currentTimeMillis()+myheadimg.getOriginalFilename().substring(myheadimg.getOriginalFilename().lastIndexOf("."));
			try {
				myheadimg.transferTo(new File(uploadDir+newFileName));
				String newFilePath="/upload/"+newFileName;
				staff.setStaff_headimg(newFilePath);
			} catch (IllegalStateException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(staff);
			staffService.uploadImg(staff);
		}
		return  "redirect:/staff/staffUpdate.jsp";
	}
}
